package Databasehelper;

import java.util.ArrayList;

import driver.ui.driver.Beans.notification;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class Notificationhelper extends SQLiteOpenHelper{
	
	private static final int DATABASE_VERSION = 2;
	private static final String dbName="driverappdb";
	private static final String NOTIFICATION_TABLE_NAME = "notifications";
	private static final String KEY_TYPE = "type";
	private static final String KEY_FROM = "sentfrom";
	private static final String KEY_TO ="sentto";
	private static final String KEY_ID="nid";
	private static final String KEY_DATE = "date";
	private static final String KEY_TIME = "time";
	private static final String KEY_SHARING = "share";
	private static final String NOTIFICATION_TABLE_CREATE =
	                "CREATE TABLE " + NOTIFICATION_TABLE_NAME + " (" +KEY_ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"+KEY_DATE+" TEXT,"+KEY_TIME+" TEXT,"+KEY_SHARING+" TEXT,"+KEY_FROM+" TEXT,"+KEY_TO+" TEXT,"+KEY_TYPE+" TEXT)";

	
	
	public Notificationhelper(Context context) {
		super(context, dbName, null,DATABASE_VERSION);
		// TODO Auto-generated constructor stub
	}

	
	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		db.execSQL(NOTIFICATION_TABLE_CREATE);	
		ContentValues cv=new ContentValues();
		cv.put(KEY_FROM, "EugeneCrow");
		cv.put(KEY_TO, "FredSchmidt");
		cv.put(KEY_TIME, "10:00am-11:00am");
		cv.put(KEY_DATE, "Thursday, September, 2011");
		cv.put(KEY_SHARING, "Location and Status");
		cv.put(KEY_TYPE, "notification");
		db.insert(NOTIFICATION_TABLE_NAME,KEY_FROM, cv);
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}
	public Boolean insertNot(notification not) {

		Boolean ok=false;
		SQLiteDatabase db=this.getWritableDatabase();
	
		ContentValues cv=new ContentValues();
		cv.put(KEY_DATE,not.getDate());
		cv.put(KEY_TIME, not.getTime());
		cv.put(KEY_SHARING,not.getShare());
		cv.put(KEY_FROM,not.getSentfrom());
		cv.put(KEY_TO,not.getSentto());
		cv.put(KEY_TYPE,not.getType());
		db.insert(NOTIFICATION_TABLE_NAME,KEY_TO, cv);
		db.close();
		ok=true;
		return ok;
	}
	public Boolean insertConfirm(notification not) {

		Boolean ok=false;
		SQLiteDatabase db=this.getWritableDatabase();
		
	
		ContentValues cv=new ContentValues();
		cv.put(KEY_DATE,not.getDate());
		cv.put(KEY_TIME, not.getTime());
		cv.put(KEY_SHARING,not.getShare());
		cv.put(KEY_FROM,not.getSentfrom());
		cv.put(KEY_TO,not.getSentto());
		cv.put(KEY_TYPE,not.getType());
		db.insert(NOTIFICATION_TABLE_NAME,KEY_FROM, cv);
		db.close();
		ok=true;
		return ok;
	}
	 public ArrayList<notification> getAllNotByTo(String name)
	 {
		 SQLiteDatabase db=this.getWritableDatabase();
		 ArrayList<notification> nots=new ArrayList<notification>();
		 notification not=new notification();
		 String[] params=new String[]{String.valueOf(name)};
		 Cursor cur= db.rawQuery("SELECT * FROM "+NOTIFICATION_TABLE_NAME+" WHERE "+KEY_TO+"=?",params);
         
         int size=cur.getCount();
         cur.moveToFirst();
         for (int i=0;i<size;i++)
         {
        	 
        	 String from=cur.getString(cur.getColumnIndex(KEY_FROM));
        	 String date=cur.getString(cur.getColumnIndex(KEY_DATE));
        	 String time=cur.getString(cur.getColumnIndex(KEY_TIME));
        	 String share=cur.getString(cur.getColumnIndex(KEY_SHARING));
        	 String type=cur.getString(cur.getColumnIndex(KEY_TYPE));
        	 cur.moveToNext();
        	 not.setSentfrom(from);
        	 not.setDate(date);
        	 not.setTime(time);
        	 not.setShare(share);
        	 not.setSentto(name);
        	 not.setType(type);
        	 nots.add(not);
        	
        	 }
         cur.close();
		 return nots;
		 
	 }

	  


}
